package ph.com.isla.sf2d.ui;

import ph.com.isla.sf2d.SF2DSurfaceView;
import ph.com.isla.sf2d.ui.SF2DObjectInterface.OnClickListener;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;
import android.util.Log;

public class SF2DButton extends SF2DObject {
	public static final int NORMAL = 1;
	public static final int FOCUSED = 2;
	public static final int PRESSED = 3;

	private String mText;
	private Rect mTextBounds;
	private int mPaddingLeft;
	private int mPaddingRight;
	private int mPaddingTop;
	private int mPaddingBottom;
	private Align mTextAlignment = Align.CENTER;

	private int mFocusColor = Color.YELLOW;
	private int mNormalColor = Color.LTGRAY;
	private int mPressedColor = Color.GREEN;

	private int mTextColor = Color.BLACK;
	private volatile int mState;

	private Paint mTextPaint = null;
	private boolean mCustomSize;
	private Paint mRectPaint = null;
	

	

	public void setState(int state) {
		mState = state;
	}

	public int getState() {
		return mState;
	}

	public void setColor(int normal, int focus, int pressed) {
		mNormalColor = normal;
		mFocusColor = focus;
		mPressedColor = pressed;
	}

	public void setSize(int width, int height) {
		mCustomSize = true;
		setWidth(width + mPaddingLeft + mPaddingRight);
		setHeight(height + mPaddingTop + mPaddingBottom);

	}

	private void initialize() {
		mRectPaint = new Paint();
		mTextPaint = new Paint();
		mTextPaint.setTextSize(mDefaultTextSize);
		mTextPaint.setTextAlign(mTextAlignment);
		mTextPaint.setColor(mTextColor);
		mTextPaint.setAntiAlias(true);
		
		mTextBounds = new Rect();
		setPadding(20, 20, 10, 10);
		mState = NORMAL;
		setVisible(true);
		
		SF2DBorder border = new SF2DBorder(mDefaultBorderWidth, mDefaultBorderColor);
		setBorder(border);
	}

	public void setText(String text) {
		mText = text;
		mTextPaint.getTextBounds(text, 0, text.length(), mTextBounds);
		if (!mCustomSize) {
			setWidth(mTextBounds.width() * 3);
			setHeight(mTextBounds.height() * 2);
		}
	}

	public void setTextColor(int color) {
		mTextPaint.setColor(color);
	}

	public int getTextColor() {
		return mTextPaint.getColor();
	}

	public void setPadding(int left, int right, int top, int bottom) {
		mPaddingLeft = left;
		mPaddingRight = right;
		mPaddingTop = top;
		mPaddingBottom = bottom;
	}

	public String getText() {
		return mText;
	}

	public SF2DButton(SF2DSurfaceView root) {
		super(root);
		initialize();
	}

	@Override
	public void draw(final Canvas canvas) {
		Log.i("SFL", "Invoked!");
		int color = 0;
		if (getState() == NORMAL) {
			color = mNormalColor;
		} else if (getState() == FOCUSED) {
			color = mFocusColor;
		} else if (getState() == PRESSED) {
			color = mPressedColor;
		}
		
		setBackgroundColor(color);

		Rect rect = getDimension();
	
		super.draw(canvas);
		canvas.drawText(mText, 0, mText.length(), rect.centerX(),
				rect.centerY() + (mTextBounds.height() / 4), mTextPaint);
	}

	@Override
	public boolean isClickable() {
		return true;
	}

	@Override
	public void onKeyDown(float x, float y) {
		mState = PRESSED;
		getSurface().postInvalidate();
	}

	@Override
	public void onKeyUp(float x, float y) {
		mState = NORMAL;
		getSurface().postInvalidate();
		new Thread(new Runnable() {
			@Override
			public void run() {
				OnClickListener listener = getOnClickListener();
				if (listener != null) {
					listener.onClick(SF2DButton.this);
				}
			}
		}).start();
	}

	@Override
	public void onMoveOut(float x, float y) {
		mState = NORMAL;
		getSurface().postInvalidate();
	}

	@Override
	public Rect getClickableArea() {
		return getDimension();
	}


}
